/**
 * Created by PQQ on 2016-10-27.
 */
package com.ywxx.dyhelper.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateUtils
{
    /**
     * yyyy-MM-dd HH:mm
     */
    public static final String DateFormat = "yyyy-MM-dd HH:mm";
    /**
     * yyyy-MM-dd
     */
    public static final String DateFormat1 = "yyyy-MM-dd";
    /**
     * MM-dd HH:mm
     */
    public static final String DateFormat2 = "MM-dd HH:mm";
    /**
     * MM-dd
     */
    public static final String DateFormat3 = "MM-dd";

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String DateFormat4 = "yyyy-MM-dd HH:mm:ss";

    /**
     * dd HH:mm
     */
    public static final String DateFormat5 = "dd HH:mm";

    /**
     * @param datestring 日期字符串
     * @param dateformat 需要转换成的时间类型
     * @return 转换好的日期格式
     */
    public static String ToDateString(String datestring, String dateformat)
    {
        return ToDateString(datestring, '/', dateformat);
    }

    /**
     * @param datestring 日期字符串
     * @param oldChar 替换的字符
     * @param dateformat 需要转换成的时间类型
     * @return 转换好的日期格式
     */
    public static String ToDateString(String datestring, char oldChar, String dateformat)
    {
        //将字符串转为日期
        Date date = null;
        try
        {
            date = new SimpleDateFormat(DateFormat, Locale.getDefault()).parse(datestring.replace(oldChar, '-'));
        } catch (Exception e)
        {
            e.printStackTrace();
            return datestring;
        }

        return new SimpleDateFormat(dateformat, Locale.getDefault()).format(date);
    }

    /**
     * @param date 日期
     * @param dateformat 需要转换成的类型
     * @return 转换好的日期格式
     */
    public static String ToDateString(Date date, String dateformat)
    {
        //将字符串转为日期
        try
        {
            return new SimpleDateFormat(dateformat, Locale.getDefault()).format(date);
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        return date.toString();
    }

    /**
     * 当前时间
     * @return
     */
    public static String DateTimeNow()
    {
        return DateTimeNow(DateFormat4);
    }

    /**
     * 当前时间
     * @return
     */
    public static String DateTimeNow(String DateFormat)
    {
        return new SimpleDateFormat(DateFormat, Locale.getDefault()).format(new Date());
    }

    /**
     * 当前时间
     * @return
     */
    public static String getDateTimeNow(String DateFormat)
    {
        return new SimpleDateFormat(DateFormat).format(new Date(System.currentTimeMillis()));
    }

    /**
     * 计算库存天数
     * @param s
     * @return
     */
    public static String getkcts(String s)
    {
        long l = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(DateFormat4);
        String billdate = sdf.format(new Date());
        try
        {
            SimpleDateFormat formatter = new SimpleDateFormat(DateFormat4);
            ParsePosition pos = new ParsePosition(0);
            ParsePosition pos1 = new ParsePosition(0);
            Date dt1 = formatter.parse(billdate, pos);
            Date dt2 = formatter.parse(s, pos1);
            System.out.println("dt1=" + dt1);
            System.out.println("dt2=" + dt2);
            l = dt1.getTime() - dt2.getTime();

            System.out.println("Hello World!=" + l);
        } catch (Exception e)
        {
            System.out.println("exception" + e.toString());
        }
        System.out.println("String.valueOf(l)" + String.valueOf(l));
        return String.valueOf(l / (24 * 60 * 60 * 1000));
    }


    /**
     * 比较两个日期的大小，日期格式为yyyy-MM-dd
     * @param str1 the first date
     * @param str2 the second date
     * @return true <br/>false
     */
    public static boolean isDateOneBigger(String str1, String str2)
    {
        boolean isBigger = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt1 = null;
        Date dt2 = null;
        try
        {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
        if (dt1.getTime() > dt2.getTime())
        {
            isBigger = true;
        }
        else if (dt1.getTime() < dt2.getTime())
        {
            isBigger = false;
        }
        return isBigger;
    }

    /**
     * 比较两个日期的大小，日期格式为yyyy-MM-dd
     * @param str1 the first date
     * @param str2 the second date
     * @return true <br/>false
     */
    public static boolean isDate2Bigger(String str1, String str2)
    {
        boolean isBigger = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt1 = null;
        Date dt2 = null;
        try
        {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e)
        {
            e.printStackTrace();
        }
        if (dt1.getTime() > dt2.getTime())
        {
            isBigger = false;
        }
        else if (dt1.getTime() <= dt2.getTime())
        {
            isBigger = true;
        }
        return isBigger;
    }

    public static long Date2Diff(String str1, String str2)
    {
        boolean isBigger = false;
        SimpleDateFormat sdf = new SimpleDateFormat(DateFormat4);
        Date dt1 = null;
        Date dt2 = null;
        try
        {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e)
        {
            e.printStackTrace();
            return -1;
        }

        return (dt1.getTime() - dt2.getTime()) / 1000;
    }
}
